package com.tvunetworks.center.device.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.model.param.PeerIdParam;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.device.annotation.Logined;
import com.tvunetworks.center.device.model.param.TimeLockSyncParam;
import com.tvunetworks.center.device.model.param.TimeLockUpdateSyncParam;
import com.tvunetworks.center.device.model.vo.DeviceIdNameAndMsg;
import com.tvunetworks.center.device.service.TimeLockEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: tvucc-aggregator
 * @description: TimeLockController
 * @author lebronchen
 * @create: 2019-03-26 10:28
 **/
@RestController
@RequestMapping("/timeLock")
public class TimeLockController {

    @Autowired
    private TimeLockEventService timeLockEventService;

    /**
     * 打开timeLock
     * @param loginUser 用户信息
     * @param param 参数
     * @return 返回状态
     */
    @PostMapping("/sync")
    public Result<String> sync(@Logined LoginUser loginUser, @RequestBody TimeLockSyncParam param){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return timeLockEventService.synchronize(loginUser.getId(), param);
    }

    /**
     * 打开timeLock
     * @param loginUser 用户信息
     * @param param 修改sync的参数
     * @return 返回状态
     */
    @PostMapping("/updateSync")
    public Result updateSync(@Logined LoginUser loginUser, @RequestBody TimeLockUpdateSyncParam param){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        String role = loginUser.getRole();
        timeLockEventService.updateSynchronize(loginUser.getId(), param,role);
        return ResultUtil.success();
    }

    /**
     * 解锁timelock
     * @param loginUser loginUser
     * @return Result
     */
    @PostMapping("/unSync")
    public Result<String> unSync(@Logined LoginUser loginUser){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return timeLockEventService.unSynchronize(loginUser.getId());
    }

    /**
     * 根据peerId获取用户信息和所有同步R的信息
     * @param loginUser 用户信息
     * @param param 修改sync的参数
     * @return 返回状态
     */
    @PostMapping("/getTimeLockMsgByPeerId")
    public Result getTimeLockMsgByPeerId(@Logined LoginUser loginUser, @RequestBody PeerIdParam param){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return ResultUtil.success(timeLockEventService.getTimeLockMsgByPeerId(param));
    }

    /**
     * 给R发信息
     * @param peerId peerId
     * @param eventId eventId
     * @param status status
     * @return Result
     */
    @PostMapping("/receiveMsg")
    public Result<String> receiveMsg(@RequestParam String peerId, @RequestParam String eventId, @RequestParam int status){
        return timeLockEventService.receiveMsg(peerId, eventId, status);
    }

    /**
     * 检查timelock的状态
     * @param loginUser loginUser
     * @param peerIds  peerIds
     * @return Result
     */
    @GetMapping("/checkDevieTimeLockStatus")
    public Result<String> checkDevieTimeLockStatus(@Logined LoginUser loginUser, @RequestParam String peerIds){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return timeLockEventService.checkDevieTimeLockStatus(loginUser.getId(), peerIds);
    }

    /**
     *
     * @param peerIds peerIds
     * @param bitrate bitrate
     * @param delay delay
     * @return Result
     */
    @PostMapping("/setBitrateAndDelay")
    public Result setBitrateAndDelay(@RequestParam String peerIds, @RequestParam int bitrate, @RequestParam int delay){
        List<DeviceIdNameAndMsg> errors = timeLockEventService.setBitrateAndDelay(peerIds, bitrate, delay);
        if (CollectionUtils.isEmpty(errors)) {
            return ResultUtil.success();
        }
        return ResultUtil.error("81100901", "SetBitrateAndDelay failed", errors);
    }

    /**
     *
     * @param loginUser loginUser
     * @param peerIds peerIds
     * @return Result
     */
    @PostMapping("/removeSyn")
    public Result<String> removeSyn(@Logined LoginUser loginUser, @RequestParam String peerIds){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return timeLockEventService.removeSyn(loginUser.getId(), peerIds);
    }
}
